<template>
  <div class="aliases-popover">
    <h1>{{formData.title}}</h1>
    <div class="input-content">
      <el-form label-width="50px">
        <el-form-item label="类别">
          <el-select  placeholder="请选择" v-model="formData.data.channelId">
            <el-option v-for="item in channels" :key="item.id" :label="item.channelName" :value="item.id">
            </el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="别名" >
          <el-input placeholder="请输入酒店别名名称" v-model="formData.data.name"></el-input>
        </el-form-item>
        <el-form-item label="英文名">
          <el-input placeholder="请输入英文名称" v-model="formData.data.enName"></el-input>
        </el-form-item>
      </el-form>
    </div>
    <el-form-item style="margin-top: 40px;">
      <el-button type="primary" @click="save">保存</el-button>
      <el-button @click="cancel">取消</el-button>
    </el-form-item>
  </div>
</template>
<script>
import ELEMENT from 'element-ui'
import { getChannels } from '@/public/js/hotelInfomation'
export default {
  props: ['datas'],
  data () {
    return {
      channels: [],
      channel: '',
      channelId: ''
    }
  },
  computed: {
    formData () {
      return this.datas
    }
  },
  beforeDestroy () {
    window.bus.$off('change')
  },
  methods: {
    save () {
      this.channels.forEach(e => {
        if (e.id === this.datas.data.channelId) {
          this.channel = e.channelName
        }
      })
      let aliasEntite = {
        'name': this.datas.data.name,
        'enName': this.datas.data.enName,
        'channel': this.channel,
        'channelId': this.datas.data.channelId
      }
      console.log('aliasEntite', aliasEntite)
    // 表单验证
      for (let i in aliasEntite) {
        if (!aliasEntite[i]) {
          ELEMENT.Message.error({
            message: '别名/类别/英文名不能为空'
          })
          return false
        }
        // 判断英文名是否合法
        let  re = new RegExp("^[a-zA-Z]+$"); 
        if (!re.test(aliasEntite.enName)) {
           ELEMENT.Message.error({
            message: '英文名必须是全英文'
          })
          return false
        } 
      }
      window.bus.$emit('change', aliasEntite)
      this.$emit('close')
    },
    cancel () {
      this.$emit('close')
    }
  },
  created () {
    // 获取别名
    getChannels().then(res => {
      this.channels = res.data.channels
    })
  }
}
</script>
<style lang="scss">
.aliases-popover {
  box-sizing: border-box;
  position: absolute;
  width: 100%;
  top: 0px;
  left: 0;
  background: #fff;
  z-index: 11;
  padding: 20px;
  box-shadow: 5px 5px 10px rgba(0, 0, 0, 0.1);
  .input-content {
    margin-top: 10px;
    .el-form-item {
      margin-bottom: 20px;
    }
    .el-input .el-input__inner {
      width: 300px;
    }
    .el-select {
      width: 300px;
    }
  }
  .el-button {
    width: 82px;
    height: 34px;
    padding: 0px;
  }
}
</style>

